package com.bgt.filter;

import java.io.IOException;
import java.util.Map;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.bgt.model.MUser;
import com.mysql.jdbc.Constants;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

public class LoginFilter  implements Filter {
	private static String  nologin="";
	public void destroy() {
		// TODO Auto-generated method stub
		
	}

	public void doFilter(ServletRequest req, ServletResponse rep, FilterChain chain)
			throws IOException, ServletException {
			req.setCharacterEncoding("UTF-8"); 
			 HttpServletRequest hreq = (HttpServletRequest) req;  
			 HttpServletResponse hrep=(HttpServletResponse)rep;
			 String uri=hreq.getServletPath();
			 String[] notCheckURLList = nologin.split(";");
			 //String[] notCheckURLList = filterConfig.getInitParameter("notCheckURLList").split(";");
			 System.out.println(uri);
			 HttpSession session=hreq.getSession();
			 MUser user=(MUser) session.getAttribute("currentUser");
			 boolean doFilter=true;
			 if(user==null){
				 for(String noCheckURL:notCheckURLList) {
					 if(uri.indexOf(noCheckURL)>=0){
						 doFilter=false;
					 }
				 }
				 if(doFilter){
					 //hrep.sendError(401, "请登录");  
					 hrep.sendRedirect("");
					 return;
				 }else{
					 chain.doFilter(req, rep);    
				 }
			 }else{
				 chain.doFilter(req, rep);    
			 }
			 
		
	}

	public void init(FilterConfig arg0) throws ServletException {
		nologin=arg0.getInitParameter("notCheckURLList");
		System.out.println(nologin);
	}  
	  
   
}  
